package com.itheima.tlias.service.impl;

import com.itheima.tlias.mapper.DeptMapper;
import com.itheima.tlias.pojo.Dept;
import com.itheima.tlias.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class DeptServiceImpl implements DeptService {
	@Autowired
	private DeptMapper deptMapper;

	@Override
	public List<Dept> list() {
		return deptMapper.list();
	}

	@Override
	public Dept getById(Integer id) {
		return deptMapper.getById(id);
	}

	@Override
	public int deleteById(Integer id) {
		return deptMapper.delete(id);
	}

	@Override
	public void save(Dept dept) {
		dept.setCreateTime(LocalDateTime.now());
		dept.setUpdateTime(LocalDateTime.now());
		deptMapper.save(dept);
	}

	@Override
	public void updateById(Dept dept) {
		Dept oldDept = deptMapper.getById(dept.getId());
		if (oldDept == null) throw new RuntimeException("没有id为" + dept.getId() + "的部门，更新失败");
		dept.setUpdateTime(LocalDateTime.now());
		deptMapper.updateById(dept);
	}
}
